//
//  HSYWithDrawViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/7.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "HSYWithDrawViewController.h"
#import "HSYBalanceInfoModel.h"
#import "BQActivityView.h"
#import "HSYGetBalanceInfoRequest.h"
#import "MZFormSheetPresentationViewController.h"
#import "HSYWithDrawCodeViewController.h"
#import "HSYWithDrawSuccessViewController.h"

@interface HSYWithDrawViewController ()<UITextFieldDelegate, HSYWithDrawCodeViewControllerDelegate>

@property(nonatomic,strong)HSYBalanceInfoModel *balanceInformation;

@property (weak, nonatomic) IBOutlet UILabel *availableMoneyLabel;
@property (weak, nonatomic) IBOutlet UILabel *bankNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *poundageLabel;
@property (weak, nonatomic) IBOutlet UILabel *actualArrivalLabel;

@property (weak, nonatomic) IBOutlet UITextField *cashWithdrawalAmountTextFiled;

@end

@implementation HSYWithDrawViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.titleButton setTitle:@"提现" forState:UIControlStateNormal];
    
    [self loadData];
    
    self.cashWithdrawalAmountTextFiled.delegate = self;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textfiledTextDidChange:) name:UITextFieldTextDidChangeNotification object:self.cashWithdrawalAmountTextFiled];
    
    self.poundageLabel.text = [NSString stringWithFormat:@"手续费：%.2f元",self.balanceInformation.poundage];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(void)loadData{
    HSYGetBalanceInfoRequest *task = [[HSYGetBalanceInfoRequest alloc] init];
    task.accountId = [[HSYUserCenter instatce].appAuUser.accountID integerValue];
    task.channel = 101;
    
    __weak typeof(self)weakSelf = self;
    [BQActivityView showActiviTy];

    [HSYNetworkManager POST:task success:^(HSYResponse *response) {
        [BQActivityView hideActiviTy];
        
        if (response.resultCode == HSY_REQUEST_CODE_SUCCESS) {
            
            HSYBalanceInfoModel *model = [HSYBalanceInfoModel mj_objectWithKeyValues:response.encryptDataResult];
            
            weakSelf.balanceInformation = model;
            
            [weakSelf refresHeaderView];
        }else{
            [weakSelf showMessage:response.resultMessage];
        }
    } failure:^(NSError *error) {
        [BQActivityView hideActiviTy];
        if (error.code != NSURLErrorCancelled) {
            [weakSelf showMessage:@"请求超时"];
        }
    }];
}

//更新账户信息（可用金额、手续费等）
-(void)refresHeaderView{
    self.poundageLabel.text = [NSString stringWithFormat:@"手续费：%.2f元",self.balanceInformation.poundage];
    self.availableMoneyLabel.text = [NSString stringWithFormat:@"%.2f",self.balanceInformation.available];
    self.bankNameLabel.text = [NSString stringWithFormat:@"%@(%@)",self.balanceInformation.bankName,self.balanceInformation.cardNo];
}





- (IBAction)test:(id)sender {
    if (!([_cashWithdrawalAmountTextFiled.text floatValue]>0)) {
        [self showMessage:@"请填入可用的金额"];
        return ;
    }
    
    if (([_cashWithdrawalAmountTextFiled.text floatValue]>self.balanceInformation.available)) {
        [self showMessage:@"请填入小于可用余额的金额"];
        return ;
    }
    
    HSYWithDrawCodeViewController *vc = [[HSYWithDrawCodeViewController  alloc] init];
    vc.delegate = self;
    vc.money = self.cashWithdrawalAmountTextFiled.text;
    
    MZFormSheetPresentationViewController *formSheetController = [[MZFormSheetPresentationViewController alloc] initWithContentViewController:vc];
    [formSheetController.presentationController setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.5]];
    formSheetController.presentationController.shouldCenterVertically = YES;
    formSheetController.contentViewCornerRadius = 0;
    formSheetController.contentViewControllerTransitionStyle = MZFormSheetPresentationTransitionStyleSlideFromBottom;

    formSheetController.presentationController.frameConfigurationHandler = ^(UIView * __nonnull presentedView, CGRect currentFrame, BOOL isKeyboardVisible) {
        if (isKeyboardVisible) {
            return CGRectMake(0, ScreenHeigth()-420, ScreenWidth(), 420);
        }else{
            return CGRectMake(0, ScreenHeigth()-379, ScreenWidth(), 379);
        }
    };
    
    [self presentViewController:formSheetController animated:YES completion:nil];
}

#pragma mark- 提现成功 -
- (void)hsyWithDrawCodeViewControllerDidPassedValidation:(HSYWithDrawCodeViewController *)viewController{
    NSString *str = [NSString stringWithFormat:@"%.2f",[_cashWithdrawalAmountTextFiled.text floatValue] - _balanceInformation.poundage];

    HSYWithDrawSuccessViewController *vc = [[HSYWithDrawSuccessViewController alloc] init];
    vc.bankName = self.balanceInformation.bankName;
    vc.cardNo = self.balanceInformation.cardNo;
    vc.amount = self.cashWithdrawalAmountTextFiled.text;
    vc.poundage = [NSString stringWithFormat:@"%.2f",self.balanceInformation.poundage];
    vc.arrivalAmount = str;
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    return YES;
}

-(void)textfiledTextDidChange:(NSNotification *)noti{
    UITextField *textFiled = noti.object;
    
    if (textFiled == self.cashWithdrawalAmountTextFiled) {
        CGFloat crash = [textFiled.text doubleValue];
        
        if (crash>self.balanceInformation.available) {
            [self showMessage:@"可用余额不足"];
        }
        
        CGFloat avAmount = crash - self.balanceInformation.poundage;
        self.actualArrivalLabel.text = [NSString stringWithFormat:@"到账金额：%.2f元",avAmount<0?0:avAmount];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
